package com.atguigu.lease.admin.controller.apartment;

import com.atguigu.lease.admin.service.CityInfoService;
import com.atguigu.lease.admin.service.DistrictInfoService;
import com.atguigu.lease.admin.service.ProvinceInfoService;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.bean.CityInfo;
import com.atguigu.lease.model.bean.DistrictInfo;
import com.atguigu.lease.model.bean.ProvinceInfo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name="区域管理模块")
@RestController
@RequestMapping("/admin/region")
public class RegionController {
    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;
    @Operation(summary = "1、查询所有省")
    @GetMapping("/province/list")
    public Result<List<ProvinceInfo>> listProvince(){
        return Result.ok(provinceInfoService.list());
    }
    ///admin/region/city/listByProvinceId?id=42
    @Operation(summary = "2、查询省id下所有的城市")
    @GetMapping("/city/listByProvinceId") //传入的是省id
    public Result<List<CityInfo>> listByProvinceId(@RequestParam Integer id) {
        //select * from city_info where is_deleted = 0 and province_id = #{id}
        return Result.ok(cityInfoService.list(Wrappers.lambdaQuery(CityInfo.class)
                .eq(CityInfo::getProvinceId, id)));
    }
    ///admin/region/district/listByCityId
    @Operation(summary = "3、查询城市id下所有的区县")
    @GetMapping("/district/listByCityId") //传入的是城市id
    public Result<List<DistrictInfo>> listByCityId(@RequestParam Integer id) {
        //select * from city_info where is_deleted = 0 and province_id = #{id}
        return Result.ok(districtInfoService.list(Wrappers.lambdaQuery(DistrictInfo.class)
                .eq(DistrictInfo::getCityId, id)));
    }
}
